package com.topvision.controller.app;

import com.topvision.model.glass.Equipment;
import com.topvision.model.glass.FaceAlarm;
import com.topvision.service.api.glass.AppFaceAlarmService;
import com.topvision.service.glass.EquipmentService;
import com.topvision.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "智能眼镜接口类", description = "包含系统设置、录像机文件上传等")
@RequestMapping("app/face")
public class AppFaceController {

    @Autowired
    AppFaceAlarmService appFaceAlarmService;

    @Autowired
    EquipmentService equipmentService;

    String base64Data;

    @RequestMapping(value = "/save/alarm", method = RequestMethod.POST)
    public R saveAlarm(@RequestBody FaceAlarm faceAlarm) {
        FaceAlarm entity = appFaceAlarmService.find(faceAlarm.getFaceNumber(), faceAlarm.getGuid());
        if (entity == null) {
            appFaceAlarmService.save(faceAlarm);
        } else {
            //更新
            entity.setScore(faceAlarm.getScore());
            entity.setAlarmTime(faceAlarm.getAlarmTime());
            entity.setSnapBase64Data(faceAlarm.getSnapBase64Data());
            entity.setFaceBase64Data(faceAlarm.getFaceBase64Data());
//            entity.setAlarmAddress(faceAlarm.getAlarmAddress());
            appFaceAlarmService.update(entity);
        }
        return R.success();
    }

    @RequestMapping(value = "/uploadImg", method = RequestMethod.GET)
    public R uploadImg(@RequestParam("base64Data") String base64Data) {
        this.base64Data = base64Data;
        return R.success(base64Data);
    }

    @RequestMapping(value = "/getImg", method = RequestMethod.GET)
    public R getImg() {
        return R.success(base64Data);
    }

    /**
     * 文件上传具体实现方法;
     *
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
//            String dstFolder = "/usr/local/videostream";
            // String dstFolder = "/Users/gushenjie/Downloads/develop/apache-tomcat-7.0.37/webapps/ROOT/face";
            //  String dstFolder =  "/Users/gushenjie/Documents/nodejs-workspace/TvWeb_UserCenter";
            String dstFolder = "/home/TvWeb_UserCenter";
            try {
                /*
                 * 这段代码执行完毕之后，图片上传到了工程的跟路径； 大家自己扩散下思维，如果我们想把图片上传到
                 * d:/files大家是否能实现呢？ 等等;
                 * 这里只是简单一个例子,请自行参考，融入到实际中可 能需要大家自己做一些思考，比如： 1、文件路径； 2、文件名；
                 * 3、文件格式; 4、文件大小的限制;
                 */
                File dstFile = new File(dstFolder, file.getOriginalFilename());
                if (!dstFile.exists()) {
                    dstFile.createNewFile();
                }
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(dstFolder,
                                file.getOriginalFilename())));
                System.out.println(file.getName());
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }

            return "上传成功";

        } else {
            return "上传失败，因为文件是空的.";
        }
    }


    @RequestMapping(value = "/loginOrOut", method = RequestMethod.POST)
    public R loginOrOut(@RequestParam("guid") String guid, @RequestParam("status") int status) {
        Equipment equipment = equipmentService.queryByGuid(guid);
        equipment.setIsdeleted(status);
        equipmentService.update(equipment);
        return R.success();
    }

}
